package com.gitee.wsl.console

object MiniPrinter {
    /**
     * 生成间断性黑块数据
     * @param w : 打印纸宽度, 单位点
     * @return
     */
    fun initBlackBlock(w: Int): ByteArray {
        val ww = (w + 7) / 8
        val n = (ww + 11) / 12
        val hh = n * 24
        val data = ByteArray(hh * ww + 5)
        data[0] = ww.toByte() //xL
        data[1] = (ww shr 8).toByte() //xH
        data[2] = hh.toByte()
        data[3] = (hh shr 8).toByte()
        var k = 4
        for (i in 0 until n) {
            for (j in 0..23) {
                for (m in 0 until ww) {
                    if (m / 12 == i) {
                        data[k++] = 0xFF.toByte()
                    } else {
                        data[k++] = 0
                    }
                }
            }
        }
        data[k++] = 0x0A
        return data
    }

    /**
     * 生成一大块黑块数据
     * @param h : 黑块高度, 单位点
     * @param w : 黑块宽度, 单位点, 8的倍数
     * @return
     */
    fun initBlackBlock(h: Int, w: Int): ByteArray {
        val ww = (w - 1) / 8 + 1
        val data = ByteArray(h * ww + 6)
        data[0] = ww.toByte() //xL
        data[1] = (ww shr 8).toByte() //xH
        data[2] = h.toByte()
        data[3] = (h shr 8).toByte()
        var k = 4
        for (i in 0 until h) {
            for (j in 0 until ww) {
                data[k++] = 0xFF.toByte()
            }
        }
        data[k++] = 0x00
        data[k++] = 0x00
        return data
    }
}